package MyCustomTable;

import MyUtil.DataBaseUtil;
import MyUtil.JScrollUtil;
import MyUtil.MyPanel;
import MyUtil.ResultSetTableModel;

import java.awt.*;
import java.sql.*;
import javax.swing.*;

public class Customer_Query_ResultSet extends JFrame{
    // 数据库相关
    private JScrollPane scrollPane;
    private ResultSetTableModel model;
    private ResultSet rs;
    private String sql="select * from Customer where";
    private Boolean t1 = true;

    //建立二个控件

    private JPanel jpanel1 = new MyPanel("/picture/3.jpg");

    //空间二的内容
    //书名
    JCheckBox jc1 = new JCheckBox("客户编号");
    JTextField jt1 = new JTextField(10);
    //出版社
    JCheckBox jc2 = new JCheckBox("客户姓名");
    JTextField jt2 = new JTextField(20);
    //作者
    JCheckBox jc3 = new JCheckBox("电话");
    JTextField jt3 = new JTextField(10);
    //类别
    JCheckBox jc4 = new JCheckBox("短信");
    JTextField jt4 = new JTextField(10);
    //等级
    JCheckBox jc5 = new JCheckBox("客户等级");
    JTextField jt5 = new JTextField(10);
    //按钮
    JButton jb1 = new JButton("查询");
    JTextArea textArea1	= new JTextArea();

    public Customer_Query_ResultSet(String name) throws SQLException {
        //设置总JFrame属性
        super(name);
        scrollPane = new JScrollPane();
        setBounds(400,200,800,700);
        setContentPane(jpanel1);
        //设置中间文本框
        jpanel1.add(textArea1);

        //设置控件二
        jpanel1.add(jc1);
        jpanel1.add(jt1);
        jpanel1.add(jc2);
        jpanel1.add(jt2);
        jpanel1.add(jc3);
        jpanel1.add(jt3);
        jpanel1.add(jc4);
        jpanel1.add(jt4);
        jpanel1.add(jc5);
        jpanel1.add(jt5);
        jpanel1.add(jb1);
        jt1.setText("");
        jt2.setText("");
        jt3.setText("");
        jt4.setText("");
        jpanel1.setLayout(new CustomLayout());
        jb1.addActionListener((e)->{
            textArea1.setText("");
            if (jc1.isSelected()){
                if ((jt1.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"缺少输入数据!");
                    t1 = false;
                }else{
                    sql +=" and"+" (Cid like'%"+jt1.getText()+"%')";
                }
            }
            if (jc2.isSelected()){
                if ((jt2.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"缺少输入数据!");
                    t1 = false;
                }else{
                    sql +=" and"+" (Cname like'%"+jt2.getText()+"%')";
                }
            }
            if (jc3.isSelected()){
                if (jt3.getText().equals("")){
                    JOptionPane.showMessageDialog(this,"缺少输入数据!");
                    t1 = false;
                }else{
                    sql +=" and"+" (Cphone like'%"+jt3.getText()+"%')";
                }
            }
            if (jc4.isSelected()){
                if ((jt4.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"输入错误!");
                    t1 = false;
                }else{
                    sql +=" and"+" (Cemail like'%"+jt4.getText()+"%')";
                }
            }
            if (jc5.isSelected()){
                if ((jt5.getText()).equals("")){
                    JOptionPane.showMessageDialog(this,"输入错误!");
                    t1 = false;
                }else{
                    sql +=" and"+" (Clevel like'%"+jt5.getText()+"%')";
                }
            }
            if (sql.endsWith("where")){
                if (t1){
                    sql = sql.replace("where","");
                    textArea1.append(sql);
                }
            }else {
                if(t1){
                    sql = sql.replaceFirst(" and", "");
                    textArea1.append(sql);
                }
            }
            if(t1){
                try {
                    jpanel1.remove(scrollPane);
                    scrollPane = JScrollUtil.getCustomJScrollPane(DataBaseUtil.produceResultSet(sql));
                    jpanel1.add(scrollPane);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            sql ="Select * from Customer where";
            t1= true;
        });

    }
    private class CustomLayout implements  LayoutManager{

        @Override
        public void addLayoutComponent(String name, Component comp) {

        }

        @Override
        public void removeLayoutComponent(Component comp) {

        }

        @Override
        public Dimension preferredLayoutSize(Container parent) {
            return null;
        }

        @Override
        public Dimension minimumLayoutSize(Container parent) {
            return null;
        }

        @Override
        public void layoutContainer(Container parent) {
            int width = getWidth();
            int height = getHeight();
            if(jc1.isVisible()){
                jc1.setBounds(width/8, height/8,100,25);
            }
            if(jt1.isVisible()){
                jt1.setBounds(width/8+110, height/8,100,25);
            }
            if(jc2.isVisible()){
                jc2.setBounds(width/8, height/8+25,100,25);
            }
            if(jt2.isVisible()){
                jt2.setBounds(width/8+110, height/8+25,100,25);
            }
            if(jc3.isVisible()){
                jc3.setBounds(width/8, height/8+50,100,25);
            }
            if(jt3.isVisible()) {
                jt3.setBounds(width / 8 + 110, height / 8 + 50, 100, 25);
            }
            if(jc4.isVisible()){
                jc4.setBounds(width/8,height/8+75,100,25);
            }
            if(jt4.isVisible()){
                jt4.setBounds(width/8+110, height/8+75,100,25);
            }
            if(jc5.isVisible()){
                jc5.setBounds(width/2,height/8,100,25);
            }
            if (jt5.isVisible()){
                jt5.setBounds(width/2+110,height/8,100,25);
            }
            if(jb1.isVisible()){
                jb1.setBounds(7*width/8-70,height/11,70,height/5);
            }

            if (textArea1.isVisible()){
                textArea1.setBounds(width/8,height*5/17,width*3/4,height/5);
            }
            if(!scrollPane.isValid()){
                scrollPane.setBounds(0,height/2,width,height/2);
            }
        }
    }
}